package lumis.doui.table.field;

import lumis.doui.DouiException;
import lumis.doui.source.Source;
import lumis.doui.source.field.type.ISourceFieldDataType;
import lumis.doui.source.field.type.SourceFieldTypes;
import lumis.doui.table.field.GenericTableSourceField;
import lumis.doui.table.field.TableSourceFieldMapping;
import lumis.portal.PortalException;
import lumis.util.query.QueryBase;
import lumis.util.query.QueryField;
import lumis.util.query.QueryTable;

import org.w3c.dom.Node;

/**
 * A generic single column table source field. The column may be in the main table
 * or in a foreign table with the given tableRelationId.
 * 
 * @since 4.0.4
 * @version $Revision: 6251 $ $Date: 2007-03-27 10:49:29 -0300 (Tue, 27 Mar 2007) $
 */
public class SingleColumnTableSourceField extends GenericTableSourceField
{
	private String tableRelationId;
	private String singleColumnName;
	private ISourceFieldDataType forcedDataType;
	private String forcedDataTypeId;

	public SingleColumnTableSourceField(String tableRelationId, String columnName, String dataTypeId) throws PortalException
	{
		this.tableRelationId = tableRelationId;
		this.singleColumnName = columnName;
		this.forcedDataType = null;
		this.forcedDataTypeId = dataTypeId;
	}
	
	public SingleColumnTableSourceField(String tableRelationId, String columnName, ISourceFieldDataType dataType)
	{
		this.tableRelationId = tableRelationId;
		this.singleColumnName = columnName;
		this.forcedDataType = dataType;
		this.forcedDataTypeId = null;
	}
		
	@Override
	public void init(Source source, Node fieldNode) throws PortalException
	{
		super.init(source, fieldNode);
		if (singleColumnName != null)
			columnName = singleColumnName;
	}
	
	@Override
	protected ISourceFieldDataType createDataType() throws PortalException
	{
		if (forcedDataType != null)
			return forcedDataType;
		else if (forcedDataTypeId != null)
			return SourceFieldTypes.getInstance(forcedDataTypeId);
		else
			return super.createDataType();
	}

	@Override
	public TableSourceFieldMapping getTableMapping(QueryBase queryBase) throws PortalException
	{
		TableSourceFieldMapping mapping = new TableSourceFieldMapping();
		if (isExternalData() || !isGetData())
			return mapping;
		
		QueryField queryField = getQueryField(queryBase);
		mapping.addField(queryField);
		return mapping;
	}
	
	@Override
	protected boolean isInsertable()
	{
		if (tableRelationId != null)
			return false;
		else
			return super.isInsertable();
	}

	@Override
	protected boolean isUpdatable()
	{
		if (tableRelationId != null)
			return false;
		else
			return super.isUpdatable();
	}

	@Override
	protected QueryField getQueryField(QueryBase queryBase) throws PortalException
	{
		QueryTable table;
		if (tableRelationId != null)
			table = queryBase.getTableByRelationId(tableRelationId);
		else
			table = queryBase.getTable();
		if (table == null)
			throw new DouiException("STR_TABLE_NOT_FOUND_FOR_RELATION_ID;" + tableRelationId);
		QueryField queryField = table.getField(getColumnName());
		queryField.setFieldAlias(getId());
		queryField.setDataType(getDataType().getQueryDataType());
		queryField.setConverter(getConverter());
		return queryField;
	}
}
